package cn.kelvin.stack;

import java.io.IOException;

import cn.kelvin.adapter.Token.TokenType;
import cn.kelvin.adapter.MyTokenStream;
import cn.kelvin.adapter.Token;
import cn.kelvin.adapter.TokenStream;

public class ExpressionTest {
	
	public static void main(String[] args) throws IOException {
		System.out.println(countExpresstion(new MyTokenStream(System.in)));;
	}
	
	
	public static int countExpresstion(TokenStream ts) throws IOException{
		MyStack<Integer> operateStack = new MyStack<>(3);
		Token token = null;
		while(ts.getToken().getTokenType() != TokenType.NONE){
			token = ts.getToken();
			if(token.getTokenType()==TokenType.INT){
				operateStack.push((Integer)token.getValue());
			}else{
				Integer ope1 = operateStack.pop();
				Integer ope2 = operateStack.pop();
				if(token.getTokenType()==TokenType.PLUS){
					operateStack.push(ope1+ope2);
				}else if(token.getTokenType()==TokenType.MINUS){
					operateStack.push(ope2-ope1);
				}else if(token.getTokenType()==TokenType.MULT){
					operateStack.push(ope1*ope2);
				}else if(token.getTokenType()==TokenType.DIV){
					operateStack.push(ope2/ope1);
				}
			}
			ts.consumeToken();
		}
		return operateStack.pop();
	}
}
